'use strict';

define(['angular', 'moment', 'angular-ui-router', '../../../common/utils/service', '../account/account', '../../member/points/points', '../../member/order/order',
	'../profile/profile', '../../login/login', '../register/register', 'readable-range'], function (angular, moment) {
	var module = angular.module('memberCenter', ['ui.router', 'dataService', 'memberAccount', 'memberPoints', 'memberOrder', 'login', 'memberProfileCenter', 'memberRegister']);
	module.config(['$stateProvider', function ($stateProvider) {
		$stateProvider
			.state("member", {
				url: '/member',
				views: {
					"@": {
						controller: 'MemberCtrl',
						templateUrl: '/mstore/member/center/center.html'
					}
				}
			})
		$stateProvider
			.state("oldMember", {
				url: '/member-center',
				views: {
					"@": {
						controller: 'OldMemberCtrl'
					}
				}
			})
	}]);
	//TODO 以后会删除
	module.controller('OldMemberCtrl', ['$state', function ($state) {
		$state.transitionTo('member')
	}])
	module.controller('MemberCtrl', ['$rootScope', '$state', '$scope', 'dataService', 'appService', function ($rootScope, $state, $scope, dataService, appService) {
		$rootScope.pageTitle = "会员中心";
		$scope.memberBound(function () {
			dataService.getMemberInfo(sessionStorage.memberId, function (err, dataObj) {
				if (err) {
					$state.transitionTo("login");
					return;
				}
				$scope.dataObj = dataObj;

				dataService.getMemberPayReq(sessionStorage.store, sessionStorage.memberId, function (err, data) {
					if (err) return;
					$scope.reqData = data;
				});

			})
		}, {force: true});
	}]);
	module.filter('friendlyTimespan', function () {
		return function (time, short) {
			if (!time) return;
			var nowTime = new Date().getTime();
			if (time) {
				var difTime = nowTime - time;
				var result = getTime(difTime, nowTime);
				if (!result) {
					return "几秒";
				}
				return result + '前';
			} else {
				return "几秒";
			}
		};
	});
	function getTime(time, nowTime) {
		var year1 = moment(moment(time + nowTime).startOf('year'));
		var year2 = moment(nowTime).startOf('year');
		var resultYear = year1.preciseDiff(year2);

		var month1 = moment(moment(time + nowTime).startOf('month'));
		var month2 = moment(nowTime).startOf('month');
		var resultMonth = month1.preciseDiff(month2);

		var day1 = moment(moment(time + nowTime).startOf('day'));
		var day2 = moment(nowTime).startOf('day');
		var resultDay = day1.preciseDiff(day2);

		var hour1 = moment(moment(time + nowTime).startOf('hour'));
		var hour2 = moment(nowTime).startOf('hour');
		var resultHour = hour1.preciseDiff(hour2);
		var result = '';
		if (resultYear) {
			result = resultYear;
		} else if (resultMonth) {
			result = resultMonth;
		} else if (resultDay) {
			result = resultDay;
		} else if (resultHour) {
			result = resultHour;
		}
		return result;
	}
});
